package com.shenmazong.delayqueue.demo01;

import com.fasterxml.jackson.annotation.JsonFormat;

import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

/**
 * @program: server-java-demo
 * @description: OrderRecord
 * @author: 亮子说编程
 * @create: 2020-11-02 14:00
 **/
public class OrderRecord implements Delayed {

    @JsonFormat(locale = "zh", timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    private long time;
    String name;

    public OrderRecord(String name, long time, TimeUnit unit) {
        this.name = name;
        this.time = System.currentTimeMillis() + (time > 0 ? unit.toMillis(time) : 0);
    }

    public long getDelay(TimeUnit unit) {
        return time - System.currentTimeMillis();
    }

    public int compareTo(Delayed o) {
        OrderRecord order = (OrderRecord) o;
        long diff = this.time - order.time;
        if (diff <= 0) {
            return -1;
        } else {
            return 1;
        }
    }
}
